#include "DataManger.h"
#include "DBService.h"
#include "DataPackage.h"
#include "OutPutMessage.h"

#include <QSqlQuery>
#include <QSqlError>

DataManger::DataManger(QDBService * db, QObject *parent)
	: QObject(parent)
{
	pDbService = db;
}

DataManger::~DataManger()
{

}

QJsonObject DataManger::requestData(const QString & method, const QString & request)
{
	if (method.compare("/SELECT", Qt::CaseInsensitive) == 0)
	{
		return select(request);
	}

	if (method.compare("/UPDATE", Qt::CaseInsensitive) == 0
		|| method.compare("/INSERT", Qt::CaseInsensitive) == 0
		|| method.compare("/DELETE", Qt::CaseInsensitive) == 0 )
	{
		return execSQL(request);
	}

	return QJsonObject();
}

QImage DataManger::downloadPicture(const QString & method, const QString & request)
{
	QImage image;
	bool ret = image.load(request);
	return image;
}

QJsonObject DataManger::savePicture(const QString & method, const QByteArray & request)
{
	QString strMsg = "";
	bool ret = savePicture(method, request, strMsg);
	QJsonObject reslut;
	if (ret)
	{
		reslut.insert("Success", strMsg);
	}
	else
	{
		reslut.insert("Error", strMsg);
	}

	return reslut;
	
}

QJsonObject DataManger::select(const QString & SQL)
{
	QJsonObject record;
	DataPackage pack;

	bool bExec = false;
	QSqlQuery * query = pDbService->select(SQL, bExec);
	if (bExec && query)
	{
		if (!pack.response(*query, record))
		{
            const QString & strError = pack.getError();
			QString strErrors = tr("Serialize Data Failed, Error: %1").arg(strError);
			pack.response(strError, true, record);
			outPutMessage(strErrors, MSG_ERROR);
		}
		else
		{
			QString strInfo = tr("Select Data: %1").arg(SQL);
			outPutMessage(strInfo);
		}

		delete query;
		query = NULL;
		return record;
	}

	if (query)
	{
		QString strError = tr("Select Data Failed! \nSQL: %1 \n Error: %2").arg(SQL).arg(query->lastError().text());
		outPutMessage(strError, MSG_ERROR);
		delete query;
		query = NULL;
		pack.response(strError, true, record);
	}
	else
	{
		QString strError = tr("Connect DB Failed!");
		outPutMessage(strError, MSG_ERROR);
		pack.response(strError, true, record);
	}

	return record;
}

QJsonObject DataManger::execSQL(const QString & SQL)
{
	QString strMsg;
	bool bExec = pDbService->execSQL(SQL, strMsg);

	QJsonObject reslut;
	if (bExec)
	{
		reslut.insert("Success", strMsg);
	}
	else
	{
		reslut.insert("Error", strMsg);
	}

	return reslut;
}

QImage DataManger::downloadPicture(const QString & filePath)
{
	QImage image;
	bool ret = image.load(filePath);
	return image;
}

bool DataManger::savePicture(const QString & method, const QByteArray & request, QString &msg)
{
	QStringList listUrl = method.split("?");
	QString strFileName = "";
	for (int i = 1; i < listUrl.count(); i++)
	{
		QString strName = listUrl[i].left(8);
		QString strName2 = listUrl[i].mid(9);
		if (listUrl[i].left(8) == "filename")
		{
			strFileName = listUrl[i].mid(9);
		}
	}

	QImage &imagePicture = QImage::fromData(request);
	imagePicture.data_ptr();
	bool ret = imagePicture.save(strFileName);
	if (!ret)
	{
		msg = "save failed";
	}
	return ret;
}

